<?php


function session_need_login($next_url = ""){

    $uid = session_user();
    if(!$uid){
        // jump to login
        $url = url_get('login');
        $url = url_append_param($url, array("f" => "nl"));

        if($next_url !== false){
            if($next_url == ""){
                // get current url as next url
                $url = url_append_param($url, array("next" => session_get_current_url()));
            } else {
                // use $next_url as next url
                $url = url_append_param($url, array("next" => $url));
            }
        } 


        url_redirect($url);
    }

    return $uid;
}


function session_user(){

    $sid = param_cookie(session_get_session_key());

    $row = session_db_get($sid);
    if(empty($row["uid"])){

        $_session_user_cache_uid = false;
        return false; 
    } else {
        $uid = $row["uid"];

        session_db_update($sid);

        $_session_user_cache_uid = $uid;
        return $uid;
    }
}

function session_create($uid, $expire = false){

    $retval = session_db_set($uid);

    if($retval){
        $sid = $retval;
        if($expire){
            $time_expire = 0;
        }else{
            $time_expire = time () + 86400 * 365;
        }
        setcookie(session_get_session_key(), $sid, $time_expire , "/", "..");
    }

    session_db_gc();

    return $retval;
}

function session_get_session_key(){
    return 'rsjy_sid';
}

function session_generate_key($uid){
    return md5(uniqid(mt_rand(), true)) . "-" . $uid;
}

function session_db_get($sid){
    if(empty($sid)){
        return false;
    }
    $db = get_db();
    $sql = "SELECT * FROM `rsjy_session` "
        . " WHERE `sid` = '".$db->escape($sid)."'"
        . " LIMIT 1 ";
    $row = $db->fetch($db->query($sql));
    if(empty($row["uid"])){
        return false;
    } else {
        return $row;
    }
}

function session_db_set($uid){
    $db = get_db();

    $sql = "SELECT * FROM `rsjy_session` "
        . " WHERE `uid` = '".$db->escape($uid)."'"
        . " LIMIT 1 ";
    $row = $db->fetch($db->query($sql));
    if(!empty($row["sid"])){
        $sid = $row["sid"];
    } else {
        $sid = session_generate_key($uid);
    }

    $sql = "INSERT INTO `rsjy_session`"
        . " (`sid`,`uid`,`time_create`,`time_active`) "
        . " VALUES "
        . " ('".$db->escape($sid)."','".$db->escape($uid)."',UNIX_TIMESTAMP(), UNIX_TIMESTAMP()) "
        . " ON DUPLICATE KEY UPDATE `time_active` = UNIX_TIMESTAMP() ";

    $rs = $db->query($sql);
    if($rs === false){
        return false;
    } else {
        return $sid;
    }
}

function session_db_update($sid){
    $db = get_db();

    $sql = "UPDATE `rsjy_session`"
        . " SET `time_active` = UNIX_TIMESTAMP() "
        . " WHERE `sid` = '".$db->escape($sid)."'";

    $rs = $db->query($sql);
    if($rs === false){
        return false;
    } else {
        return true;
    }
}

function session_db_gc(){
    $db = get_db();

    $sql = "DELETE FROM `rsjy_session`"
        . " WHERE `time_active` < ( UNIX_TIMESTAMP() - 3600 ) "
        . " LIMIT 100 ";

    $rs = $db->query($sql);
    if($rs === false){
        return false;
    } else {
        return true;
    }
}

function session_db_get_online_num(){
    $db = get_db();

    $sql = "SELECT COUNT(*) AS online_num FROM `rsjy_session`";

    $rs = $db->fetch($db->query($sql));
    if(!empty($rs["online_num"])){
        return $rs["online_num"];
    } else {
        return 0;
    }
}


function session_get_current_url(){
    return "http://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
}


